package com.quartz.springbootquartz.simple;

import org.quartz.*;
import org.quartz.impl.StdSchedulerFactory;

/**
 * 如何实现quartz动态添加
 *
 * @Author longdeng
 * @Date 2020/7/27 0027 21:51
 */
public class JobDemo3 {

    public static void main(String[] args) throws SchedulerException, InterruptedException {
        JobDetail jobDetail = JobBuilder.newJob(MyJob.class)
                .withIdentity("myJob", "myGroup")
                .usingJobData("name", "zhxl")
                .build();

        JobDetail jobDetail2 = JobBuilder.newJob(MyJob2.class)
                .withIdentity("myJob2", "myGroup")
                .usingJobData("name", "zhxl")
                .build();

        Trigger trigger = TriggerBuilder.newTrigger()
                // 触发器标识
                .withIdentity("trigger1", "trigger1")
                .withSchedule(SimpleScheduleBuilder.simpleSchedule()
                        .withIntervalInSeconds(2).repeatForever())
                .startNow()
                .build();

        Trigger trigger2 = TriggerBuilder.newTrigger()
                // 触发器标识
                .withIdentity("trigger2", "trigger1")
                .withSchedule(SimpleScheduleBuilder.simpleSchedule()
                        .withIntervalInSeconds(2).repeatForever())
                .startNow()
                .build();

        SchedulerFactory factory = new StdSchedulerFactory();
        Scheduler scheduler = factory.getScheduler();

        scheduler.scheduleJob(jobDetail2, trigger2);
        scheduler.start();

//        Thread.sleep(30);
        scheduler.scheduleJob(jobDetail, trigger);
//        scheduler.start();

//        Thread.sleep(10);
        scheduler.pauseJob(new JobKey("myJob", "myGroup"));
    }

}
